#include "ack_msg.h"
#include <stdio.h>
#include "msg_def.h"
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>


#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>

#include <linux/un.h>
#include <sys/socket.h>
#include <fcntl.h>

extern int g_msg_id_lua;


void sendAckToLua(void * ackDataIn)
{
    printf("enter in sendAckToLua\n");
    if(g_msg_id_lua<0)
    {
        key_t key = ftok(msgLuaQueuePath, keyMsg_LUA);
        if(key < 0)
        {
            perror("ftok");
            return;
        }

        int msg_id = msgget(key, IPC_CREAT| 0600);
        if(msg_id < 0)
        {
            perror("msgget");
        } 
        g_msg_id_lua = msg_id;
    } 
    recvDataEleCtl * pstAckData = (recvDataEleCtl *)ackDataIn;
    if(g_msg_id_lua<0)
    {        
        return ;
    }
    else
    {
        msgAckLua stMsgQueueAck;

        memset(&stMsgQueueAck,0,sizeof(msgAckLua));
        stMsgQueueAck.type = QUEUE_MSG_TYPE_LUA;
        memcpy(&stMsgQueueAck.eleStatus,pstAckData,sizeof(recvDataEleCtl));
        
        if(msgsnd(g_msg_id_lua, (void*)&stMsgQueueAck, sizeof(recvDataEleCtl), 0) < 0)
        {
            printf("send ack to lua failed\n");
            g_msg_id_lua = -1;
            sendAckToLua(ackDataIn);
        }
        else
        {
            printf("send ack to lua success,msg_id:%d\n",g_msg_id_lua);
        }
        
    }    

}


void sendAckToLua(int sidIn, int status)
{
    recvDataEleCtl stAckData;// = (recvDataEleCtl *)ackDataIn; 
    memset(&stAckData,0,sizeof(recvDataEleCtl));

    stAckData.sid = sidIn;
    stAckData.frameType = status;

    sendAckToLua(&stAckData);  
}

int clientNotify = -1;

int initNotifyClientSock(void)
{
    int                 fd;
    struct sockaddr_un  serveraddr;

    printf("enter in initSockClient\n");
 
    fd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (fd < 0) {
        fprintf(stderr, "socket: %s\n", strerror(errno));
        return -1;
    }
 
    serveraddr.sun_family = AF_UNIX;
    snprintf(serveraddr.sun_path, sizeof(serveraddr.sun_path), "%s", SOCK_IPC_NOTIFY);
    
    int flags=fcntl(fd,F_GETFL,0);
    fcntl(fd,F_SETFL,flags|O_NONBLOCK);//设置为非阻塞

    int result = connect(fd, (struct sockaddr *)&serveraddr, (socklen_t)sizeof(struct sockaddr_un));
    if (result)
    {
        perror("ops: client\n");
        //exit(1);
        return -1;
    }
    else
    {
        clientNotify = fd;
    }
    return 0;
}


void sendSockNotifyMsg(void * stMsgIn)
{
    if(clientNotify==-1)
    {
        int ret = initNotifyClientSock();

        if(ret)
        {
            printf("init notify client sock failed\n");
            return;
        }
    }
    else
    {
        fd_set rfds;
        struct timeval tv;
        int retval;
        tv.tv_sec = 0;
        tv.tv_usec = 10;

        FD_ZERO(&rfds);
        
        FD_SET(clientNotify, &rfds);

        retval = select(clientNotify+1,&rfds,NULL,NULL,&tv);

        if(retval<0)
        {
            perror("select()");
            close(clientNotify);
            clientNotify = -1;
        }
        else if(retval==0)
        {
            //no data available in 2 second;
        }
        else
        {
            //data received;
            if(FD_ISSET(clientNotify, &rfds))
            {
                int recvLen = 0;
                char recvBuff[128] = {0};
                recvLen = read(clientNotify,recvBuff,128);

                if(recvLen>0)
                {              

                }
                else
                {
                    //the romote sock closed.
                    close(clientNotify);
                    clientNotify = -1;
                }
                
            }
        }
        if(clientNotify==-1)
        {
           sendSockNotifyMsg(stMsgIn); 
        }
    }

    char dataToSend[128] = {0};
    int currOffset = 0;
    report_up_msg * msgReport = (report_up_msg *)stMsgIn;
    
    memcpy(dataToSend,&msgReport->type,sizeof(int));
    currOffset+=sizeof(int);
    memcpy(dataToSend+currOffset,&msgReport->dataLen,sizeof(int));
    currOffset+=sizeof(int);

    if(msgReport->type == NOTIFY_DJCLEAN_WATER_LESS)
    {
        memcpy(dataToSend+currOffset,&msgReport->un_msg_data.waterLessMask,sizeof(int));
        currOffset += sizeof(int);
    }
    else if(msgReport->type == NOTIFY_SCAN_CODE)
    {
        memcpy(dataToSend+currOffset,&msgReport->un_msg_data.barcodeChan,sizeof(int));
        currOffset += sizeof(int);
    }
    else if(msgReport->type == NOTIFY_QPCR_PHOTO)
    {
        memcpy(dataToSend+currOffset,&msgReport->un_msg_data.qpcrNotify,sizeof(int));
        currOffset += sizeof(int);        
    }
    else if(msgReport->type == NOTIFY_CURR_TEMP)
    {
        memcpy(dataToSend+currOffset,&msgReport->un_msg_data.tempReportMsg,sizeof(temp_report_msg));
        currOffset += sizeof(temp_report_msg);  
    }


    if(write(clientNotify,dataToSend,currOffset)==-1)
    {
        printf("send report msg failed\n");
    }
}